package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.model.Option;
import com.youlai.system.converter.PositionConverter;
import com.youlai.system.mapper.SysPositionMapper;
import com.youlai.system.model.entity.SysPosition;
import com.youlai.system.model.entity.SysUser;
import com.youlai.system.model.form.PositionForm;
import com.youlai.system.model.query.PositionPageQuery;
import com.youlai.system.model.vo.PositionPageVO;
import com.youlai.system.service.SysPositionService;
import com.youlai.system.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 岗位业务接口实现
 */
@Service
@RequiredArgsConstructor
public class SysPositionServiceImpl extends ServiceImpl<SysPositionMapper, SysPosition> implements SysPositionService {

    private final PositionConverter positionConverter;

    private final SysUserService userService;

    @Override
    public SysPosition getByContentEqual(String content) {
        LambdaQueryWrapper<SysPosition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPosition::getName, content);
        return getOne(queryWrapper);
    }

    @Override
    public Long saveByContent(String content) {
        SysPosition position = new SysPosition();
        position.setName(content);
        save(position);
        return position.getId();
    }

    @Override
    public Page<PositionPageVO> getPositionPage(PositionPageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();

        LambdaQueryWrapper<SysPosition> queryWrapper = new LambdaQueryWrapper<SysPosition>()
                .and(StrUtil.isNotBlank(keywords),
                        wrapper ->
                                wrapper.like(StrUtil.isNotBlank(keywords), SysPosition::getName, keywords)
                                        .or()
                                        .like(StrUtil.isNotBlank(keywords), SysPosition::getCode, keywords)
                );
        //查询数据
        Page<SysPosition> positionPage = this.page(new Page<>(pageNum, pageSize), queryWrapper);
        //设置人员数量和人员列表
        Map<Long, SysUser> positionUserCountMap = userService.positionUserCountMap();
        positionPage.getRecords().forEach(position -> {
            SysUser user = positionUserCountMap.get(position.getId());
            if (user != null) {
                position.setUserCount(user.getCount());
                position.setUserList(user.getGroupNickName());
            }
        });
        return positionConverter.entity2Page(positionPage);
    }

    @Override
    public boolean savePosition(PositionForm positionForm) {
        String name = positionForm.getName();
        long nameCount = this.count(new LambdaQueryWrapper<SysPosition>().eq(SysPosition::getName, name));
        Assert.isTrue(nameCount == 0, "岗位名称已存在");

        String code = positionForm.getCode();
        long codeCount = this.count(new LambdaQueryWrapper<SysPosition>().eq(SysPosition::getCode, code));
        Assert.isTrue(codeCount == 0, "岗位编号已存在");

        // 实体转换
        SysPosition position = positionConverter.form2Entity(positionForm);
        return save(position);
    }

    @Override
    public boolean updatePosition(Long positionId, PositionForm positionForm) {
        String name = positionForm.getName();
        long nameCount = this.count(new LambdaQueryWrapper<SysPosition>()
                .eq(SysPosition::getName, name)
                .ne(SysPosition::getId, positionId)
        );
        Assert.isTrue(nameCount == 0, "岗位名称已存在");

        String code = positionForm.getCode();
        long codeCount = this.count(new LambdaQueryWrapper<SysPosition>()
                .eq(SysPosition::getCode, code)
                .ne(SysPosition::getId, positionId));
        Assert.isTrue(codeCount == 0, "岗位编号已存在");

        // form -> entity
        SysPosition entity = positionConverter.form2Entity(positionForm);

        // 修改岗位
        return this.updateById(entity);
    }

    @Override
    public PositionForm getPositionForm(Long positionId) {
        SysPosition entity = this.getById(positionId);
        return positionConverter.entity2Form(entity);
    }

    @Override
    public boolean deletePositions(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "岗位删除数据为空");
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public boolean deletePositions(List<Long> idList) {
        return this.removeByIds(idList);
    }

    @Override
    public List<Option> listPositionOptions() {
        // 查询数据
        List<SysPosition> positionList = this.list(new LambdaQueryWrapper<SysPosition>()
                .select(SysPosition::getId, SysPosition::getName)
                .orderByAsc(SysPosition::getSort)
        );

        // 实体转换
        return positionConverter.entities2Options(positionList);
    }
}
